package  
{
  import flash.display.Sprite;
  import flash.events.Event;
  import mge.graph.AStar;
  import mge.graph.Edge;
  import mge.graph.Graph;
  import mge.graph.Node;
	/**
   * ...
   * @author ivan
   */
  public class Main extends Sprite
  {
    private var graphStage:GraphStage;
    public static var background:Sprite;
    public function Main() 
    {
      background = new Sprite();
      addChild(background);
      
      graphStage = new GraphStage();      
      addChild( graphStage );
      
      // randomGraph();
      //lineGraph(3);
      // boardGraph(8);
    }
    
    private function boardGraph(n:int):void
    {
      var graph:Graph = new Graph();
      
      for (var i:int = 0; i < n*n; i++)
      {
        var nx:int = 300 + (i % n) * 40;
        var ny:int = 20 + (int)(i / n) * 40;
        var node:Node = new Node(i.toString() + "_main", nx, ny);
        graph.nodes.push(node);
        graphStage.addNode(node);
      }
      
      for (i = 0; i < n; i++)
      for (var j:int = 0; j < n-1; j++)
      {
        var edge:Edge = new Edge(graph.nodes[i * n + j], graph.nodes[i * n + j + 1]);
        graph.edges.push(edge);
        graphStage.addEdge(edge);
      }
      
      for (i = 0; i < n - 1; i++)
      for (j = 0; j < n; j++)
      {
        edge = new Edge(graph.nodes[i * n + j], graph.nodes[(i + 1) * n + j]);
        graph.edges.push(edge);
        graphStage.addEdge(edge);
      }
    }
    
    private function lineGraph(n:int):void
    {
      var graph:Graph = new Graph();
      
      for (var i:int = 0; i < n; i++)
      {
        var node:Node = new Node(i.toString() + "_main", 100 + i*30, 30);
        graph.nodes.push(node);
        graphStage.addNode(node);
      }
      
      for (i = 0; i < n - 1; i++)
      {
        var edge:Edge = new Edge(graph.nodes[i], graph.nodes[i + 1]);
        graph.edges.push(edge);
        graphStage.addEdge(edge);
      }
    }
    
    private function randomGraph():void
    {
      var graph:Graph = new Graph();
      
      for (var i:int = 0; i < 64; i++)
      {
        var nx:int = 300 + (i % 8) * 40;
        var ny:int = 20 + (int)(i / 8) * 40;
        var node:Node = new Node(i.toString() + "_main", nx, ny);
        graph.nodes.push(node);
        graphStage.addNode(node);
      }
      
      for (i = 0; i < 100; i++)
      {
        var source:Node = graph.nodes[(int)(Math.random() * graph.nodes.length)];
        var target:Node = graph.nodes[(int)(Math.random() * graph.nodes.length)];
        var edge:Edge = new Edge(source, target);
        graph.edges.push(edge);
        graphStage.addEdge(edge);
      }
    }
    
  } 

}